<?php
namespace app\teacher\controller;

use think\Db;

class Exercise extends Base {

    //作业库列表
    public function paper_list(){
        $param = request()->get();
        $where = [];
        if(isset_full($param,'title')){
            $where['title'] = ['like','%'.trim($param['title']).'%'];
        }
        $uid = $this->teacher_id();
        $lists = Db::name('exam_papers')->where($where)->where('FIND_IN_SET('.$uid.', share_ids)')->field('id,title,question_ids,createtime')->select();
        if($lists){
            foreach ($lists as $key => $value){
                $ids = explode(',',$value['question_ids']);
                $lists[$key]['question_num'] = count($ids);
                $lists[$key]['createtime'] = date('Y-m-d',$value['createtime']);
            }
        }
        return messages(200, '请求成功', $lists);
    }

    //作业库详情
    public function paper_detail(){
        $id = request()->get('papers_id');
        $where = [];
        if($id){
            $where['id'] = $id;
        }else{
            return messages(201, '参数错误');
        }
        //查询详情
        $data = Db::name('exam_papers')->where($where)->field('id,question_ids')->find();
        if($data['question_ids']){
            $ids = explode(',',$data['question_ids']);
            $res = [];
            foreach ($ids as $value){
                $res[] = Db::name('exam_question')->where(['id'=>$value])->find();
            }
            $data['question'] = $res;
        }else{
            $data['question'] = '';
        }
        return messages(200, '请求成功', $data);
    }

    //发布页
    public function get_publish(){
        $id = request()->get('papers_id');
        $where = [];
        if($id){
            $where['id'] = $id;
        }else{
            return messages(201, '参数错误');
        }
        //查询详情
        $data = Db::name('exam_papers')->where($where)->field('id,title')->find();
        return messages(200, '请求成功', $data);
    }

    //发布
    public function publish(){
        $uid = $this->teacher_id();
        $param = request()->post();
        $times = time();
        if(isset_full($param,'papers_id')){
            $data = Db::name('exam_papers')->where(['id'=>$param['papers_id']])->find();
            if($data){
                $res['question_ids'] = $data['question_ids'];
                $res['papers_id'] = $param['papers_id'];
                $res['uid'] = $data['uid'];
                $res['profile_id'] = $uid;
            }else{
                return messages(201, '当前题不存在');
            }
        }else{
            return messages(201, '参数错误');
        }
        if(isset_full($param,'title')){
            $res['title'] = $param['title'];
        }else{
            return messages(201, '请输入标题');
        }
        if(isset_full($param,'start_time')){
            $res['start_time'] = strtotime($param['start_time']);
        }else{
            return messages(201, '请选择开始时间');
        }
        if(isset_full($param,'end_time')){
            $res['end_time'] = strtotime($param['end_time']);
        }else{
            return messages(201, '请选择结束时间');
        }
        if(isset_full($param,'num')){
            $res['num'] = $param['num'];
        }else{
            return messages(201, '请选择答题次数');
        }
        if(isset_full($param,'classes_id')){
            $res['classes_id'] = $param['classes_id'];
        }else{
            return messages(201, '请选择班级');
        }
        if(isset_full($param,'answer_time')){
            $res['answer_time'] = $param['answer_time'];
            $res2['ans_time'] = $param['answer_time'];
        }else{
            return messages(201, '请输入答题时间');
        }
        $res['createtime'] = $times;
        try {
            // 开启事务
            Db::startTrans();
            $rs = Db::name('exam_papers_gl')->insertGetId($res);
            $user = Db::name('classes_student')->where(['classes_id'=>$param['classes_id']])->field('uid')->select();
            if($user){
                $res2['papers_id'] = $param['papers_id'];
                $res2['papers_gl_id'] = $rs;
                $res2['start_time'] = $times;
                $res2['num'] = 1;
                $res2['class_id'] = $param['classes_id'];
                foreach ($user as $key => $value){
                    $res2['user_id'] = $value['uid'];
                    Db::name('exam_user_papers')->insert($res2);
                }
            }else{
                // 回滚事务
                Db::rollback();
                return messages(201, '发布失败');
            }
            // 提交事务
            Db::commit();
            return messages(200, '发布成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            // 处理异常，比如记录日志、返回错误信息等
            return messages(201, '发布失败');
        }
    }

    //发布记录
	public function lists (){
        $uid = $this->teacher_id();
		$lists = Db::name('exam_papers_gl')->where(['type'=>1,'profile_id'=>$uid])->field('id,title,createtime')->select();
	    if($lists){
            foreach ($lists as $key => $value){
                $lists[$key]['createtime'] = date('Y-m-d',$value['createtime']);
            }
        }
        return messages(200, '请求成功', $lists);
    }

    //预览
    public function preview(){
        $papers_id = request()->get('papers_id');
        $quesion_ids = Db::name('exam_papers_gl')->where(['id'=>$papers_id])->value('question_ids');
        $res = [];
        if($quesion_ids){
            $ids = explode(',',$quesion_ids);
            foreach ($ids as $key => $value) {
                $quesion = Db::name('exam_question')->where(['id'=>$value])->find();
                $res[$key]['id'] = $quesion['id'];
                $res[$key]['kind'] = $quesion['kind'];
                $res[$key]['answer'] = $quesion['answer'];
                $res[$key]['describe'] = $quesion['describe'];
                $res[$key]['difficulty'] = $quesion['difficulty'];
                $res[$key]['options_json'] = json_decode($quesion['options_json']);
                $res[$key]['other_name'] = $quesion['other_name'];
                $res[$key]['other_url'] = $quesion['other_url'];
                $res[$key]['title'] = $quesion['title'];

            }
        }
        return messages(200, '请求成功', $res);
    }

    //作业概述
    public function overview(){
        $papers_id = request()->get('papers_id');
        $data = Db::name('exam_papers_gl')->alias('a')
            ->join('fa_classes_class b','a.classes_id=b.id')
            ->field('a.id,a.title,a.classes_id,a.profile_id,a.question_ids,a.start_time,a.end_time,a.end_time,a.num,b.title as class_name')
            ->where(['a.id'=>$papers_id])
            ->find();
        if($data){
            $question_ids = count(explode(',',$data['question_ids']));
            $data['start_time'] = $data['start_time'] ? date('Y-m-d H:i:s',$data['start_time']) : '';
            $data['end_time'] = $data['end_time'] ? date('Y-m-d H:i:s',$data['end_time']) : '';
            //学生人数
            $data['class_student'] = Db::name('classes_student')->where(['classes_id'=>$data['classes_id']])->count();
            //答题人数数据
            $dati = Db::name('exam_user_achievement')->where(['user_papers_id'=>$data['id']])->group('user_id')->select();
            //答题人数
            $data['answer_num'] = count($dati);
            //没有答题的人数
            $data['no_answer_num'] = $data['class_student'] - $data['answer_num'];
            $user_badge = Db::name('user_badge')->alias('a')
                ->join('fa_classes_student b','a.user_id=b.uid')
                ->field('user_id,b.realname')
                ->where(['a.classes_id'=>$data['classes_id'],'papers_id'=>$data['id'],'profile_id'=>$data['profile_id']])
                ->select();

            $data['user_badge'] = $user_badge;//获取徽章的学生名称
            //未完成学生人名
            $no_student_name = Db::name('exam_user_papers')->alias('a')
                ->join('fa_classes_student b','a.user_id=b.uid')
                ->field('a.user_id,b.realname')
                ->where(['a.class_id'=>$data['classes_id'],'a.papers_gl_id'=>$data['id'],'a.status'=>1])
                ->select();
            $data['no_student_name'] = $no_student_name;
            //允许提交次数百分比
            if($dati){
                $i = 0;
                foreach ($dati as $dak => $dav) {
                    $dacount = Db::name('exam_user_achievement')->where(['user_id' => $dav['user_id'], 'user_papers_id' => $data['id']])->count();
                    if ($dacount == $data['num']) {
                        $i++;
                    }
                }
            }
            //提交次数百分比
            $data['tjcs_bfb'] = round(($i / $data['class_student']) *  100);
            //首次提交正确率
            $sc_bfb = Db::name('exam_user_achievement')->where(['user_papers_id'=>$data['id']])->group('user_id')->field('score')->select();
            $sc_zql = 0;
            if($sc_bfb){
                foreach ($sc_bfb as $sck => $scv){
                    $sc_zql += round($scv['score'] / $question_ids * 100);
                }
                $sc_zql = round($sc_zql / $data['class_student']);
            }
            $data['sc_zql'] = $sc_zql;
            //平均分
            $pingjunfen = 0;
            if($dati){
                foreach ($dati as $dak => $dav) {
                    $dadata = Db::name('exam_user_achievement')->where(['user_id' => $dav['user_id'], 'user_papers_id' => $data['id']])->field('score')->select();
                    foreach ($dadata as $value){
                        $pingjunfen += round(($value['score'] / $question_ids) * 100);
                    }
                }
                $pingjunfen = round($pingjunfen / $data['class_student']);
            }
            $data['pjf'] = $pingjunfen;
        }
        return messages(200, '请求成功', $data);
    }

    public function zhengli($data){
        $res['one'] = round($data[0]['score'] / ($data[0]['score'] + $data[0]['wrong']) * 100);
        $res['two'] = 0;
        $res['grade'] = 0;
        if(count($data) == 2){
            $res['one'] = round($data[0]['score'] / ($data[0]['score'] + $data[0]['wrong']) * 100);
            $res['two'] = round($data[1]['score'] / ($data[1]['score'] + $data[1]['wrong']) * 100);
        }
        return $res;
    }

    //成绩单
    public function report_card(){
        $papers_id = request()->get('papers_id');
        $data = Db::name('exam_papers_gl')->where(['id'=>$papers_id])->field('num,classes_id')->find();
        $num = $data['num'];
        $student = Db::name('classes_student')->alias('a')
            ->join('fa_classes_class b','a.classes_id=b.id')
            ->field('a.id,a.student_number,a.uid,a.realname,b.title')
            ->order('a.student_number asc')
            ->where(['a.classes_id'=>$data['classes_id']])->select();
        $lists = [];
        if($student){
            foreach ($student as $key => $value){
                $res['id'] = $value['id'];
                $res['student_number'] = $value['student_number'];
                $res['realname'] = $value['realname'];
                $res['title'] = $value['title'];
                $res['state'] = 2;
                $res['one'] = 0;
                $res['two'] = 0;
                $achievement = Db::name('exam_user_achievement')->where(['user_id'=>$value['uid'],'user_papers_id'=>$papers_id])->select();
                if($achievement){
                    if(count($achievement) == $num){
                        $res['state'] = 1;
                    }
                    $zl = $this->zhengli($achievement);
                    $res['one'] = $zl['one'];
                    $res['two'] = $zl['two'];
                }
                $lists[$key] = $res;
            }
        }
        return messages(200, '请求成功', $lists);
    }

    //成绩单
//    public function report_card(){
//        $papers_id = request()->get('papers_id');
//        $num = Db::name('exam_papers_gl')->where(['id'=>$papers_id])->value('num');
//        $student = Db::name('exam_user_achievement')->group('user_id')->where(['user_papers_id'=>$papers_id])->field('user_id')->select();
//        $data = [];
//        if($student){
//            foreach ($student as $key => $value){
//
//                $res = Db::name('classes_student')->where(['uid'=>$value['user_id']])->field('student_number,realname,classes_id')->find();
//                $res['title'] = Db::name('classes_class')->where(['id'=>$res['classes_id']])->value('title');
//                //查询该同学答题详情
//                $achievement = Db::name('exam_user_achievement')->where(['user_id'=>$value['user_id'],'user_papers_id'=>$papers_id])->order('id asc')->select();
//                if(count($achievement) > 1){
//                    $zl = $this->zhengli($achievement);
//                    $res['one'] = $zl['one'];
//                    $res['two'] = $zl['two'];
//                    //$res['grade'] = $zl['grade'];
//                }else{
//                    $res['one'] = round($achievement[0]['score'] / ($achievement[0]['score'] + $achievement[0]['wrong']) * 100);
//                    $res['two'] = 0;
//                    //$res['grade'] = $achievement[0]['grade'];
//                }
//                if($num == 2 && count($achievement) == 2){
//                    $res['state'] = 1;
//                }else if($num == 1 && count($achievement) == 1){
//                    $res['state'] = 1;
//                }else{
//                    $res['state'] = 2;
//                }
//                $data[$key] = $res;
//            }
//        }
//        return messages(200, '请求成功', $data);
//    }


}